package person.aoao.library.controller;


import org.springframework.web.bind.annotation.*;
import person.aoao.library.entity.Cards;
import person.aoao.library.entity.ReaderCount;
import person.aoao.library.entity.UserList;
import person.aoao.library.entity.WillList;
import person.aoao.library.result.ResponseCode;
import person.aoao.library.result.ResponseData;
import person.aoao.library.service.BorrowListService;
import person.aoao.library.service.CardsService;
import person.aoao.library.service.UserListService;
import person.aoao.library.service.WillListService;
import person.aoao.library.vo.ListData;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("reader")
public class LoginController {

    @Resource
    private  UserListService userListService;
    @Resource
    private BorrowListService borrowListService;

    @Resource
    private WillListService willListService;

    @Resource
    private CardsService cardsService;



//    用户端登录接口
    @PostMapping("login")
    public ResponseData login(@RequestBody ReaderCount readerCount){
        UserList reader=userListService.reqLogin(readerCount.getUid(),readerCount.getPassword());
        if(reader==null){
            return new ResponseData(ResponseCode.FAIL_INFO);
        }
        if(reader.getState()==0){
            return new ResponseData(ResponseCode.ACCOUNT_STATE);
        }
        return new ResponseData(ResponseCode.SUCCESS,reader);
    }


    @PostMapping("register")
    public  ResponseData register(@RequestBody UserList userList){
        UserList info=userListService.insert(userList);
        if(info!=null){
            return new ResponseData(ResponseCode.SUCCESS,info);
        }
        return new ResponseData(ResponseCode.Number_ERROR,info);
    }

    //用户数据修改
    @PostMapping("updateReader")
    public  ResponseData updateReader(@RequestBody UserList reader){
        UserList info=userListService.update(reader);
        return new ResponseData(ResponseCode.SUCCESS,info);
    }

    //获取用户借阅记录
    @GetMapping("borrowList/{uid}/{page}/{limit}")
    public ResponseData getBorrorwList(@PathVariable String uid, @PathVariable Integer limit, @PathVariable Integer page){
        ListData list=borrowListService.getBorrowList(uid,page,limit);
        return  new ResponseData(ResponseCode.SUCCESS,list);
    }

    //获取用户预约记录
    @GetMapping("willOrder/{uid}/{page}/{limit}")
    public ResponseData getWillOreder(@PathVariable String uid, @PathVariable Integer page, @PathVariable Integer limit){
        List<WillList> orders=this.willListService.getOreders(uid,page,limit);
        return  new ResponseData(ResponseCode.SUCCESS,orders);
    }

    //获取打卡
    @PostMapping("playCard")
    public ResponseData palyCard(@RequestBody Cards cards){
        Cards info=this.cardsService.playCard(cards);
        return  new ResponseData(ResponseCode.SUCCESS,info);
    }

    //执行打卡
    @PostMapping("playCardWithAddress")
    public ResponseData playCardWithAddress(@RequestBody Cards cards){
        Cards info=this.cardsService.update(cards);
        return new ResponseData(ResponseCode.SUCCESS,info);
    }




}
